package com.smalls.redshoes.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.utils.Constant;

public class AutoHideLayout extends FrameLayout {
    private static String TAG = Constant.baddcoolio2tag?Constant.TAGPREFIXSTR + AutoHideLayout.class.getSimpleName():AutoHideLayout.class.getSimpleName();
    protected boolean isAutoHide;
    protected long mDelayHideTime;
    protected Runnable mHideRunnable;
    protected OnVisibilityChangedListener onVisibilityChangedListener;

    public interface OnVisibilityChangedListener {
        void onVisibilityChanged(View view, int i);
    }

    public AutoHideLayout(Context context) {
        this(context, null);
    }

    public AutoHideLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AutoHideLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mDelayHideTime = 10000L;
        this.isAutoHide = true;
        init(context);
    }

    protected void init(Context context) {
        this.mDelayHideTime = onSetDelayHideTime();
        this.mHideRunnable = new Runnable() {
            public void run() {
                LogUtil.d(AutoHideLayout.TAG, "mHideRunnable=" + AutoHideLayout.this.getVisibility());
                if (AutoHideLayout.this.getVisibility() == VISIBLE) {
                    LogUtil.d(AutoHideLayout.TAG, "isAutoHide=" + AutoHideLayout.this.isAutoHide());
                    if (AutoHideLayout.this.isAutoHide()) {
                        AutoHideLayout.this.setVisibility(INVISIBLE);
                    } else {
                        AutoHideLayout.this.delayHide();
                    }
                }
            }
        };
    }

    public boolean isAutoHide() {
        return this.isAutoHide;
    }

    public void setAutoHide(boolean autoHide) {
        this.isAutoHide = autoHide;
    }

    protected long onSetDelayHideTime() {
        return 10000L;
    }

    public void setVisibility(int visibility) {
        LogUtil.d(TAG, "setVisibility=" + visibility);
        super.setVisibility(visibility);
        excuteAnim(visibility);
        removeCallbacks(this.mHideRunnable);
        if (visibility == 0) {
            postDelayed(this.mHideRunnable, this.mDelayHideTime);
        }
        if (this.onVisibilityChangedListener != null) {
            this.onVisibilityChangedListener.onVisibilityChanged(this, visibility);
        }
    }

    protected void excuteAnim(int visibility) {
    }

    public void delayHide() {
        LogUtil.d(TAG, "delayHide");
        removeCallbacks(this.mHideRunnable);
        postDelayed(this.mHideRunnable, this.mDelayHideTime);
    }

    public void destory() {
        removeCallbacks(this.mHideRunnable);
    }

    public void setOnVisibilityChangedListener(OnVisibilityChangedListener onVisibilityChangedListener) {
        this.onVisibilityChangedListener = onVisibilityChangedListener;
    }
}
